home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 304.adf / DocSplit / docsplit.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  4KB  |  189 lines

  1. /*
  2.  * docsplit : split 1.3 autodoc files into individual subroutine files
  3.  *            by Joel Swank January 8 1989
  4.  *           Version 1.0
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <exec/types.h>
  10. #define MAXBUF 100     /* size of string buffer */
  11. #define STDIN 0   /* file descriptor for stdin */
  12.  
  13. char buffer[MAXBUF];
  14. char name[200];
  15. char path[MAXBUF] = "";
  16. char buff[MAXBUF] = "";
  17. int overrite = FALSE;
  18. int verbose = FALSE;
  19. FILE *fdopen();
  20.  
  21. int fileflag = FALSE;    /* detect no files - use stdin */
  22.  
  23. main(argc,argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     register FILE    *filep ;
  28.     register cnt ;
  29.  
  30.         /* get command line options */
  31.     getcmd(argc, argv);
  32.  
  33.         /* get command line filenames */
  34.     for (cnt=1; cnt < argc; cnt++)
  35.     {    if (*argv[cnt] != '-')
  36.         {
  37.             if ((filep = fopen(argv[cnt], "r")) == NULL)
  38.                 fatal("can't open %s", argv[cnt]) ;
  39.  
  40.             printf("FILE: %s\n",argv[cnt]);
  41.             dofile(filep);
  42.             fclose(filep);
  43.             fileflag = TRUE;
  44.         }
  45.     }
  46.  
  47.     if (!fileflag)  /* if no files given, use stdin */
  48.     {
  49.         filep = fdopen(STDIN, "r");
  50.         dofile(filep);
  51.         fclose(filep);
  52.     }
  53.     exit(0);
  54. }
  55.  
  56. /*
  57.  * process open file via stream pointer
  58.  */
  59.  
  60. dofile(filep)
  61. FILE *filep;
  62. {
  63.     FILE *outfile;
  64.     struct FileLock *lock, *Lock();
  65.  
  66.     skipl(filep);
  67.  
  68.     while ((fgets(buff,MAXBUF,filep)) != NULL)
  69.         {
  70.         char *p, *t, *rindex();;
  71.         p = rindex(buff,'/');
  72.         if (p != NULL) /* if delimiter is found */
  73.             {
  74.             p++;
  75.             t = rindex(p,'\n');       /* wipe newline */
  76.             if (t != NULL) *t = '\0';
  77.             if (path[0] != '\0') /* if a path is specified */
  78.                 {
  79.                 strcpy (name,path);      /* build name */
  80.                 strcat(name,p);
  81.                 }
  82.             else strcpy(name,p);
  83.             strcat(name,".doc");
  84.             lock = Lock(name,ACCESS_READ);
  85.             if (lock != NULL && !overrite) /* file exists, skip */
  86.                 {
  87.                 fprintf(stdout,"docsplit: FILE: %s - Already Exists\n",name);
  88.                 skipl(filep);
  89.                 UnLock(lock);
  90.                 continue;
  91.                 }
  92.             if (lock) UnLock(lock);
  93.             outfile = fopen(name,"w");
  94.             if (outfile == NULL)
  95.                 {
  96.                 fprintf(stdout,"docsplit: FILE: %s - Open Failed\n",name);
  97.                 skipl(filep);
  98.                 continue;
  99.                 }
  100.             while ((fgets(buff,MAXBUF,filep)) != NULL)
  101.                 {
  102.                 if (buff[0] == '\f') break;
  103.                 fputs(buff,outfile);
  104.                 }
  105.             fclose(outfile);
  106.             if (verbose) fprintf(stderr,"File: %s - Created\n",name);
  107.             } else skipl(filep);
  108.         }
  109. }
  110.  
  111. skipl(filep)
  112. FILE *filep;
  113. {
  114.     while ((fgets(buff,MAXBUF,filep)) != NULL)
  115.         if (buff[0] == '\f') break;
  116. }
  117.  
  118. /*
  119.  *  fatal - print standard error msg and halt process
  120.  */
  121. fatal(ptrn, data1, data2)
  122. register char    *ptrn, *data1, *data2 ;
  123. {
  124.     printf("ERROR: ");
  125.     printf(ptrn, data1, data2) ;
  126.     putchar('\n');
  127.     exit(1);
  128. }
  129.  
  130.     
  131.  
  132.  
  133. /*
  134.  *  getcmd - get arguments from command line 
  135.  */
  136. getcmd(argc, argv)
  137. register argc ;
  138. register char    *argv[] ;
  139. {
  140.     register cnt ;
  141.     int end;
  142.                     /* get command options */
  143.     for (cnt=1; cnt < argc; cnt++)
  144.     {    if (*argv[cnt] == '-')
  145.         {    switch(argv[cnt][1])
  146.             {
  147.                case 'p':
  148.                 strncpy(path,&argv[cnt][2],MAXBUF);
  149.                 end = strlen(path)-1;
  150.                 if (path[end] != '/' && path[end] != '\0' && path[end] != ':' )
  151.                     strcat(path,"/") ;
  152.                 break ;
  153.  
  154.                case 'v':
  155.                 verbose = TRUE;
  156.                 break ;
  157.  
  158.                case 'o':
  159.                 overrite = TRUE;
  160.                 break ;
  161.  
  162.                case '?':                    /* help option */
  163.                  usage();
  164.                  printf(" DocSplit Ver 1.0 - Split 1.3 autodoc files.\n");
  165.                  printf(" Options:\n");
  166.                  printf(" pstr - Path to output directory. Default current.\n");
  167.                  printf(" v    - verbose - display all file names.\n");
  168.                  printf(" o    - overwrite existing files.\n");
  169.                  printf(" ?    - display this list.\n");
  170.                 exit(0);
  171.  
  172.                default:
  173.                  usage();
  174.                  exit(0);
  175.             }
  176.         }
  177.     }
  178.  
  179. }
  180.  
  181.  
  182.  
  183. usage()
  184. {
  185. printf("usage:docsplit [-pstr] [-v] [-o] [-?] [file ...]\n");
  186. }
  187.  
  188.  
  189.